package com.zdkj.subway.stm.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zdkj.common.core.constant.CommonConstants;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.dto.in.SwStInputPassengerWorkConfirmDTO;
import com.zdkj.subway.stm.dto.in.SwStInputPassengerWorkConfirmSelectDTO;
import com.zdkj.subway.stm.entity.*;
import com.zdkj.subway.stm.mapper.SwStInputPassengerWorkConfirmMapper;
import com.zdkj.subway.stm.service.*;
import com.zdkj.subway.stm.util.BeanUtilsStm;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @support: zdkj
 * @author: cuilei
 * @version: 1.0
 * @Date: 2020-06-10 10:10
 * @Description: <p>
 * 乘客事务确认
 * </p>
 */
@Service
@AllArgsConstructor
public class SwStInputPassengerWorkConfirmServiceImpl implements SwStInputPassengerWorkConfirmService {
    private final static String CRASH_WORK = "crash_work";
    private final static String ELECTRON_WORK = "electron_work";
    private final static String FREE_WORK = "free_work";
    private final static String PAY_WORK = "pay_work";
    private final static String REFUND_WORK = "refund_work";
    private final static String SPECIAL_WORK = "special_work";
    private final static String TICKET_WORK = "ticket_work";
    private final static String OTHER_WORK = "other_work";
    private final SwStInputPassengerWorkConfirmMapper swStInputPassengerWorkConfirmMapper;
    private final SwStInputPassengerWorkCashService swStInputPassengerWorkCashService;
    private final SwStInputPassengerWorkElectronService swStInputPassengerWorkElectronService;
    private final SwStInputPassengerWorkFreeService swStInputPassengerWorkFreeService;
    private final SwStInputPassengerWorkPayService swStInputPassengerWorkPayService;
    private final SwStInputPassengerWorkRefundService swStInputPassengerWorkRefundService;
    private final SwStInputPassengerWorkSpecialService swStInputPassengerWorkSpecialService;
    private final SwStInputPassengerWorkTicketService swStInputPassengerWorkTicketService;
    private final SwStInputPassengerWorkOtherService swStInputPassengerWorkOtherService;
    private final SwStInputPassengerWorkService swStInputPassengerWorkService;

    @Override
    public Map<String, List> selectList(SwStInputPassengerWorkConfirmSelectDTO selectDTO) {
        selectDTO.setConductorCode(BeanUtilsStm.bomOrJobNoSort(selectDTO.getConductorCode()));//售票员工号排序
        Map<String, List> map = new HashMap<>(8);
        if(selectDTO.getStartTime() == null) {
            selectDTO.setStartTime(LocalDate.now());
        }

        String lineCode = SecurityUtils.getUser().getLineCode();
        String stationCode = SecurityUtils.getUser().getStationCode();
        List<SwStInputPassengerWork> inputPassengerWork = swStInputPassengerWorkConfirmMapper
                .getInputPassengerWork(selectDTO,lineCode, stationCode);

        List<SwStInputPassengerWorkCash> cashList = new ArrayList<>();
        List<SwStInputPassengerWorkElectron> electronList = new ArrayList<>();
        List<SwStInputPassengerWorkPay> payList = new ArrayList<>();
        List<SwStInputPassengerWorkFree> freeList = new ArrayList<>();
        List<SwStInputPassengerWorkRefund> refundList = new ArrayList<>();
        List<SwStInputPassengerWorkSpecial> specialList = new ArrayList<>();
        if(0 != inputPassengerWork.size()) {
            inputPassengerWork.forEach(item -> {
                Integer relationId = item.getId();
                cashList.addAll(swStInputPassengerWorkCashService.list(Wrappers
                        .<SwStInputPassengerWorkCash>query().lambda()
                        .eq(SwStInputPassengerWorkCash::getRelationId, relationId)));
                electronList.addAll(swStInputPassengerWorkElectronService.list(Wrappers.
                        <SwStInputPassengerWorkElectron>query().lambda()
                        .eq(SwStInputPassengerWorkElectron::getRelationId, relationId)));
                payList.addAll(swStInputPassengerWorkPayService.list(Wrappers
                        .<SwStInputPassengerWorkPay>query().lambda()
                        .eq(SwStInputPassengerWorkPay::getRelationId, relationId)));
                freeList.addAll(swStInputPassengerWorkFreeService.list(Wrappers
                        .<SwStInputPassengerWorkFree>query().lambda()
                        .eq(SwStInputPassengerWorkFree::getRelationId, relationId)));
                refundList.addAll(swStInputPassengerWorkRefundService.list(Wrappers
                        .<SwStInputPassengerWorkRefund>query().lambda()
                        .eq(SwStInputPassengerWorkRefund::getRelationId, relationId)));
                specialList.addAll(swStInputPassengerWorkSpecialService.list(Wrappers
                        .<SwStInputPassengerWorkSpecial>query().lambda()
                        .eq(SwStInputPassengerWorkSpecial::getRelationId, relationId)));
            });
        }
        map.put(CRASH_WORK, cashList);
        map.put(ELECTRON_WORK, electronList);
        map.put(PAY_WORK, payList);
        map.put(FREE_WORK, freeList);
        map.put(REFUND_WORK, refundList);
        map.put(SPECIAL_WORK, specialList);

        List<SwStInputPassengerWorkTicket> ticketList = swStInputPassengerWorkConfirmMapper
                .getTicketWork(selectDTO, lineCode,stationCode);
        List<SwStInputPassengerWorkOther> otherList = swStInputPassengerWorkConfirmMapper
                .getOtherWork(selectDTO,lineCode, stationCode);
        map.put(TICKET_WORK, ticketList);
        map.put(OTHER_WORK, otherList);

        return map;
    }

    @Override
    public boolean confirm(SwStInputPassengerWorkConfirmDTO confirmDTO) {
        if(null != confirmDTO.getPassengerWorkIds() && 0 != confirmDTO.getPassengerWorkIds().size()) {
            List<SwStInputPassengerWork> confirmList = new ArrayList<>();
            confirmDTO.getPassengerWorkIds().forEach(id -> {
                SwStInputPassengerWork confirmItem = new SwStInputPassengerWork();
                confirmItem.setId(id);
                confirmItem.setConfirmStatus(CommonConstants.CONFIRM);
                confirmList.add(confirmItem);
            });
            swStInputPassengerWorkService.updateBatchById(confirmList);
        }

        if(null != confirmDTO.getTicketWorkIds() && 0 != confirmDTO.getTicketWorkIds().size()) {
            List<SwStInputPassengerWorkTicket> confirmList = new ArrayList<>();
            confirmDTO.getTicketWorkIds().forEach(id -> {
                SwStInputPassengerWorkTicket confirmItem = new SwStInputPassengerWorkTicket();
                confirmItem.setId(id);
                confirmItem.setConfirmStatus(CommonConstants.CONFIRM);
                confirmList.add(confirmItem);
            });
            swStInputPassengerWorkTicketService.updateBatchById(confirmList);
        }

        if(null != confirmDTO.getOtherWorkIds() && 0 != confirmDTO.getOtherWorkIds().size()) {
            List<SwStInputPassengerWorkOther> confirmList = new ArrayList<>();
            confirmDTO.getOtherWorkIds().forEach(id -> {
                SwStInputPassengerWorkOther confirmItem = new SwStInputPassengerWorkOther();
                confirmItem.setId(id);
                confirmItem.setConfirmStatus(CommonConstants.CONFIRM);
                confirmList.add(confirmItem);
            });
            swStInputPassengerWorkOtherService.updateBatchById(confirmList);
        }

        return true;
    }
}
